package cn.lisens.serial.web;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.lisens.serial.service.SerialPortService;

@RestController
@RequestMapping("/api/at-command")
public class ATCommandController {

	private final SerialPortService serialPortService;

    public ATCommandController(SerialPortService serialPortService) {
        this.serialPortService = serialPortService;
    }

    @PostMapping("/send")
    public String sendCommand(@RequestParam String port, 
                             @RequestParam int baudRate,
                             @RequestParam String command) {
        try {
            serialPortService.initSerialPort(port, baudRate);
            String response = serialPortService.sendATCommand(command, 1000);
            return "响应: " + response;
        } finally {
            serialPortService.closePort();
        }
    }
}
